//
//  WXMainViewController.m
//  weibo-OC
//
//  Created by 王旭 on 16/5/18.
//  Copyright © 2016年 王旭. All rights reserved.
//

#import "WXMainViewController.h"

#import "WXHomeTableViewController.h"
#import "WXMessageTableViewController.h"
#import "WXDiscoverTableViewController.h"
#import "WXProfileTableViewController.h"

#import "WXTabBar.h"

@interface WXMainViewController ()

@end

@implementation WXMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor redColor];
    
    [self addChildViewControllers];
    
    [self loadTabBar];
    
    //--------------------以下为测试代码--------------------
//    //设置tabBar的全局外观属性
//    id tabBarAppearance = [UITabBar appearance];
//    [tabBarAppearance setTintColor:[UIColor orangeColor]];
//    
//    WXHomeTableViewController *homeVC = [[WXHomeTableViewController alloc]init];
//    homeVC.title = @"首页";
//    [homeVC.tabBarItem setTitleTextAttributes:@{
//                                                NSForegroundColorAttributeName:[UIColor orangeColor]
//                                                }
//                                     forState:UIControlStateSelected];
//    homeVC.tabBarItem.image = [[UIImage imageNamed:@"tabbar_home"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//    homeVC.tabBarItem.selectedImage = [[UIImage imageNamed:@"tabbar_home_selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//    [self addChildViewController:homeVC];
    //--------------------以上为测试代码--------------------
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -
#pragma mark 方法调用

/**
 *  设置自定义tabBar
 */
- (void)loadTabBar {
    WXTabBar *myTabBar = [[WXTabBar alloc]init];
    [self setValue:myTabBar forKey:@"tabBar"];
    myTabBar.addButtonBlock = ^(){
        NSLog(@"我被点击了");
    };
}

/**
 *  添加四个子控制器
 */
- (void)addChildViewControllers {
    
    //实例化
    WXHomeTableViewController *homeVC = [[WXHomeTableViewController alloc]init];
    WXMessageTableViewController *messageVC = [[WXMessageTableViewController alloc]init];
    WXDiscoverTableViewController *discoverVC = [[WXDiscoverTableViewController alloc]init];
    WXProfileTableViewController *profileVC = [[WXProfileTableViewController alloc]init];
    
    //添加
    [self addChildViewController:homeVC title:@"首页" image:@"tabbar_home"];
    [self addChildViewController:messageVC title:@"消息" image:@"tabbar_message_center"];
    [self addChildViewController:discoverVC title:@"发现" image:@"tabbar_discover"];
    [self addChildViewController:profileVC title:@"我" image:@"tabbar_profile"];
    
}

/**
 *  添加子控制器
 *
 *  @param childController 子控制器
 *  @param title           标题
 *  @param imageNamed      图片名
 */
- (void)addChildViewController:(UIViewController *)childController title:(NSString *)title image:(NSString *)imageNamed {
    
    //设置文本
    childController.title = title;
    
    //设置文本颜色
    [childController.tabBarItem setTitleTextAttributes:@{
                    NSForegroundColorAttributeName:[UIColor orangeColor]
                                                        }
                                       forState:UIControlStateSelected];
    
    
    //--------------------以下为测试代码--------------------
    //设置文本字体,颜色
    //    [childController.tabBarItem setTitleTextAttributes:@{
    //                    NSForegroundColorAttributeName:[UIColor orangeColor],
    //                    NSFontAttributeName:[UIFont systemFontOfSize:30]
    //                                                         }
    //                                              forState:UIControlStateSelected];
    
    //图片包含文字状态
    //    childController.tabBarItem.imageInsets = UIEdgeInsetsMake(5, 0, -5, 0);
    //--------------------以上为测试代码--------------------
    

    
    //设置图片
    UIImage *normalImg = [UIImage imageNamed:imageNamed];
    NSString *selectStr = [NSString stringWithFormat:@"%@_selected",imageNamed];
    UIImage *selectedImg = [UIImage imageNamed:selectStr];
    
    //不渲染
    normalImg = [normalImg imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    selectedImg = [selectedImg imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    childController.tabBarItem.image = normalImg;
    childController.tabBarItem.selectedImage = selectedImg;
    

    
    //添加子控制器
    [self addChildViewController:childController];
    
}

@end
